package www.xudan.javabasedemo.model.Proxy.DynamicsProxy.horizontal;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import www.xudan.javabasedemo.MainActivity;

/**
 * Created by ${XuDan} on 2020/3/23.
 */

interface DIE {
    void haha();
}

class BEIDAILI implements DIE{

    @Override
    public void haha() {
        System.out.println("haha");
    }
}
//代理
public class HorizontalProxy {

        //获取代理类
        public static Object getinstance(DIE beidaili) {


        return Proxy.newProxyInstance(
                beidaili.getClass().getClassLoader(),
                beidaili.getClass().getInterfaces(),
                new MyInvocationHandler(beidaili)
                );
    }
}

class MyInvocationHandler implements InvocationHandler {

    private DIE beidaili;

    public MyInvocationHandler(DIE beidaili) {
        this.beidaili = beidaili;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("===============================");
        Object value = method.invoke(beidaili, args);
        System.out.println("-------------------------------");
        return value;
    }
}
class Client {
    public static void main(String[] args) {
        DIE beidaili = new BEIDAILI();

        DIE daili = (DIE) HorizontalProxy.getinstance(beidaili);

        daili.haha();
    }
}